/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.sync.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.SyncDataTraceException;
import com.pitop.sync.dao.SyncDataTraceDAO;
import com.pitop.sync.entity.SyncDataTraceState;
import com.pitop.sync.entity.SyncDataTrace;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pitop.test.entity.TestUnit;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("syncDataTraceSV")
@Slf4j
public class SyncDataTraceSVImpl extends BaseSVImpl<SyncDataTrace, Long> implements SyncDataTraceSV {

    @Autowired
    private SyncDataTraceDAO syncDataTraceDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return syncDataTraceDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(SyncDataTrace entity) throws BaseException {
        entity.setCreateTime(new Date());
        super.save(entity);
    }

    /**
     * 保存同步数据
     *
     * @param syncTraceCode
     * @param data
     */
    @Override
    public void saveSyncDataTrace(String syncTraceCode, Object data) {
        SyncDataTrace syncDataTrace = new SyncDataTrace();
        //数据库字段:syncTraceCode  属性显示:同步追踪编码
        syncDataTrace.setSyncTraceCode(syncTraceCode);
        //数据库字段:data  属性显示:数据
        syncDataTrace.setData(JSON.toJSONString(data));
        syncDataTrace.setCreateTime(new Date());
        save(syncDataTrace);
    }

    /**
     * 加载一个对象SyncDataTrace
     *
     * @param id id* @param syncTraceCode 同步追踪编码
     * @return SyncDataTrace
     */
    @Override
    public SyncDataTrace load(java.lang.Long id, java.lang.String syncTraceCode) {
        if (id == null && syncTraceCode == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("syncTraceCode", syncTraceCode);
        return syncDataTraceDAO.load(param);
    }

    /**
     * 加载一个对象SyncDataTrace 通过id
     *
     * @param id id
     * @return SyncDataTrace
     */
    @Override
    public SyncDataTrace loadById(java.lang.Long id) {
        if (id == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncDataTraceDAO.loadById(id);
    }

    /**
     * 加载一个对象SyncDataTrace 通过syncTraceCode
     *
     * @param syncTraceCode 同步追踪编码
     * @return SyncDataTrace
     */
    @Override
    public SyncDataTrace loadBySyncTraceCode(java.lang.String syncTraceCode) {
        if (syncTraceCode == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncDataTraceDAO.loadBySyncTraceCode(syncTraceCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 SyncDataTrace 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, SyncDataTraceState newState, SyncDataTraceState... oldStates) {
        if (id == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        syncDataTraceDAO.updateStateById(id, newState.name(), states);
    }

    /**
     * 根据主键syncTraceCode,oldStates 共同更新 SyncDataTrace 的状态到newState状态
     *
     * @param syncTraceCode 同步追踪编码
     * @param newState      新状态
     * @param oldStates     旧状态集合
     */
    @Override
    public void updateStateBySyncTraceCode(java.lang.String syncTraceCode, SyncDataTraceState newState, SyncDataTraceState... oldStates) {
        if (syncTraceCode == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        syncDataTraceDAO.updateStateBySyncTraceCode(syncTraceCode, newState.name(), states);
    }

    /**
     * 根据主键id 更新 SyncDataTrace 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, SyncDataTraceState state) {
        if (id == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        syncDataTraceDAO.updateById(id, state.name());
    }

    /**
     * 根据主键syncTraceCode 更新 SyncDataTrace 的状态到另一个状态
     *
     * @param syncTraceCode 同步追踪编码
     * @param state         状态
     */
    @Override
    public void updateBySyncTraceCode(java.lang.String syncTraceCode, SyncDataTraceState state) {
        if (syncTraceCode == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        syncDataTraceDAO.updateBySyncTraceCode(syncTraceCode, state.name());
    }


    /**
     * 删除对象SyncDataTrace
     *
     * @param id id* @param syncTraceCode 同步追踪编码
     * @return SyncDataTrace
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String syncTraceCode) {
        if (id == null && syncTraceCode == null) {
            throw new SyncDataTraceException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("syncTraceCode", syncTraceCode);
        syncDataTraceDAO.delete(param);
    }


    /**
     * 查询SyncDataTrace分页
     *
     * @param syncDataTrace 对象
     * @param offset        查询开始行
     * @param limit         查询行数
     * @return List<SyncDataTrace>
     */
    @Override
    public List<SyncDataTrace> list(SyncDataTrace syncDataTrace, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (syncDataTrace != null) {
            map = JSON.parseObject(JSON.toJSONString(syncDataTrace, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return syncDataTraceDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(SyncDataTrace syncDataTrace) {
        Map<String, Object> map = null;
        if (syncDataTrace != null) {
            map = JSON.parseObject(JSON.toJSONString(syncDataTrace, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return syncDataTraceDAO.count(map);
    }

    /**
     * 查询SyncDataTrace分页
     *
     * @param id     id* @param syncTraceCode  同步追踪编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<SyncDataTrace>
     */
    @Override
    public List<SyncDataTrace> list(java.lang.Long id, java.lang.String syncTraceCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("syncTraceCode", syncTraceCode);
        return syncDataTraceDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String syncTraceCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("syncTraceCode", syncTraceCode);
        return syncDataTraceDAO.count(param);
    }

}
